/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lejos.example;

import lejos.nxt.*;

/**
 *
 * @author Steed
 */
public class UltraStopHitting
{

    private final SensorPort ultrasonicPort;
    private final UltrasonicSensor ultrasonicSensor;

    public static void main(String[] args)
    {
        final UltraStopHitting robot;
        robot = new UltraStopHitting();
        robot.run();
    }

    UltraStopHitting()
    {
        ultrasonicPort = SensorPort.S1;
        ultrasonicSensor = new UltrasonicSensor(ultrasonicPort);
    }

    public void run()
    {
        Motor.B.setSpeed(360);
        Motor.C.setSpeed(360);
        moveForward();
        waitForDistance(10);

    }

    private int waitForDistance(final int max)
    {
        int distance;
        do
        {
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException ex)
            {

            }
            distance=ultrasonicSensor.getDistance();

        }
        while(distance>max);
            return distance;
    }

    private void moveForward()
    {
        Motor.B.forward();
        Motor.C.forward();
    }
}


